package com.lcj.dr.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcj.dr.controller.form.DriverLockDownForm;
import com.lcj.dr.db.dao.DriverLockdownDao;
import com.lcj.dr.db.pojo.DriverLockdownEntity;
import com.lcj.dr.service.DriverLockDownService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class DriverLockDownServiceImpl implements DriverLockDownService {

    @Resource
    private DriverLockdownDao driverLockdownDao;

    /**
     * 新增禁闭信息
     */
    @Override
    public Integer add(DriverLockDownForm driverLockDownForm) {
        DriverLockdownEntity driverLockDown = convertToEntity(driverLockDownForm);
        int rows = driverLockdownDao.add(driverLockDown);
        return rows;
    }

    /**
     * 删除禁闭信息
     */
    @Override
    public Integer delete(DriverLockDownForm form) {
        int rows = driverLockdownDao.deleteById(form.getId());
        return rows;
    }

    /**
     * 更新禁闭信息
     */
    @Override
    public Integer update(DriverLockDownForm driverLockDownForm) {
        DriverLockdownEntity driverLockDown = convertToEntity(driverLockDownForm);
        int rows = driverLockdownDao.update(driverLockDown);
        return rows;
    }

    /**
     * 查询禁闭信息
     */
    @Override
    public DriverLockdownEntity get(DriverLockDownForm form) {
        DriverLockdownEntity driverLockDown = driverLockdownDao.getById(form.getId());
        return driverLockDown;
    }

    /**
     * 查询禁闭信息列表
     */
    @Override
    public PageInfo<DriverLockdownEntity> list(DriverLockDownForm form) {
        Map<String, Object> param = BeanUtil.beanToMap(form);
        PageHelper.startPage(form.getPage(), form.getLength());
        List<DriverLockdownEntity> driverLockDownList = driverLockdownDao.list(param);
        PageInfo<DriverLockdownEntity> pageInfo = new PageInfo<>(driverLockDownList);
        return pageInfo;
    }

    private DriverLockdownEntity convertToEntity(DriverLockDownForm driverLockDownForm) {
        DriverLockdownEntity driverLockDown = new DriverLockdownEntity();
        BeanUtils.copyProperties(driverLockDownForm, driverLockDown);
        return driverLockDown;
    }
}